﻿using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Web.Configuration;

namespace msBsc.AppLogic.DataAlc
{
    public class CBizDjkRouteDetail : CustBizObject
    {
        private CDmDjkRouteDetail m_CDmDjkRouteDetail = new CDmDjkRouteDetail();
        private CDmDjkRouteDetail oDmDjkRouteDetail
        {
            get { return m_CDmDjkRouteDetail; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizDjkRouteDetail()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkRouteDetail.DJK_ROUTE_DETAIL);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["ROUTE_DETAIL_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {
            //using (msBsc.AppLogic.DataAlc.CBizShpPageColumn objtem = new CBizShpPageColumn())
            //{
            //    DataSet ds = objtem.SelectByPageNo(this.GetRowStr(drw, KeyFieldName));
            //    for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            //    {
            //        ds.Tables[0].Rows[i].Delete();
            //    }
            //    objtem.Update(ds);
            //} 
            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }

        /// <summary>
        /// 添加路线详情
        /// </summary>
        /// <param name="routeid">路线id</param>
        /// <param name="routeName">路线名称</param>
        /// <param name="detailId">景点id s</param>
        /// <param name="infoType">景点类型</param>
        /// <param name="day">第几天</param>
        /// <param name="createUserid">创建人</param>
        /// <returns></returns>
        /// Author:蒲崇彬
        /// Creation date:2015-9-22 18:05
        public string AddRouteDetail(string routeid, string routeName, string detailId, string infoType, int day, string createUserid)
        {
            DataTable dt = new DataTable();
            DataSet dsRoute = new CDmDjkRouteDetail();

            string[] details = detailId.Split(',');
            foreach (var item in details)
            {
                if (string.IsNullOrEmpty(item.Trim())) { continue; }

                //获取系统景点 
                using (CBizPushScenic pushSecenic = new CBizPushScenic())
                {
                    dt = pushSecenic.GetByID(item);
                }
                if (dt == null || dt.Rows.Count < 1)
                {
                    return "该景点不存在";
                }

                DataRow drRoute = dsRoute.Tables[0].NewRow();
                drRoute["ROUTE_DETAIL_ID"] = this.DataObj.NewID();
                //所属线路编号
                drRoute["ROUTE_ID"] = routeid;
                drRoute["ROUTE_NME"] = routeName;

                //线路详情信息编号
                drRoute["DETAIL_INFO_ID"] = dt.Rows[0]["SCENIC_ID"];
                drRoute["DETAIL_INFO_NME"] = dt.Rows[0]["SCENIC_NAME"];
                drRoute["DETAIL_INFO_PIC"] = dt.Rows[0]["SCENIC_DEFAULT_PIC"];

                drRoute["DETAIL_INFO_TYPE"] = infoType;
                drRoute["DETAIL_BELONG_DAY"] = day;

                drRoute["DETAIL_IS_SHOW"] = 1;
                drRoute["DETAIL_IS_VALID"] = 1;
                drRoute["CREATE_ID"] = createUserid;
                drRoute["CREATE_DT"] = this.dtCurrDateTime;

                dsRoute.Tables[0].Rows.Add(drRoute);
            }

            this.Update(dsRoute);
            return "OK";
        }

        /// <summary>
        /// 修改显示状态
        /// </summary>
        /// <param name="datailid">景点id</param>
        /// <param name="userid">修改人</param>
        /// <param name="status">0、不显示，1、显示</param>
        /// <returns></returns>
        /// Author:蒲崇彬
        /// Creation date:2015-9-23
        public string UpdateStatus(string datailid, string userid, string status)
        {

            DataSet ds = GetByID(datailid);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
            {
                return "该景点未添加到路线";
            }
            DataRow routRows = ds.Tables[0].Rows[0];
            routRows["DETAIL_IS_SHOW"] = status;
            routRows["MODIFY_ID"] = userid;
            routRows["MODIFY_DT"] = this.dtCurrDateTime;
            ds.Tables[0].TableName = this.TableName;
            this.Update(ds);
            return "OK";

        }

        /// <summary>
        /// 删除景点
        /// </summary>
        /// <param name="datailid">景点id</param>
        /// <returns></returns>
        /// Author:蒲崇彬
        /// Creation date:2015-9-23
        public string DeleteData(string datailid)
        {
            if (string.IsNullOrEmpty(datailid))
            {
                return "该景点未添加到路线";
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("datailid", ParameterDirection.Input, EDbType.String, 36, datailid));
            this.DataObj.QueryBySQL(@"DELETE DJK_ROUTE_DETAIL WHERE ROUTE_DETAIL_ID=@datailid", parmCollection);

            return "OK";
        }

        /// <summary>
        /// 获取路线
        /// </summary>
        /// <param name="datailid">景点id</param>
        /// <returns></returns>
        /// Author:蒲崇彬
        /// Creation date:2015-9-23
        public DataSet GetByID(string datailid)
        {
            if (string.IsNullOrEmpty(datailid))
            {
                return null;
            }


            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("datailid", ParameterDirection.Input, EDbType.String, 36, datailid));

            return this.DataObj.QueryBySQL(@"SELECT * FROM DJK_ROUTE_DETAIL WHERE ROUTE_DETAIL_ID=@datailid", parmCollection);
        }

    }
}
